<?php
/**
 * quick_curd
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/07/12
 * 描述: api_xxx 代表前端接口,其它代表后端接口
 */

namespace app\result\auto;

use app\model\auto\NoteBooks;
use app\result\BaseResult;


class NoteBooksResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new NoteBooks();
    }

    //列表页面配置
    public function list_config()
    {
         return $this->success_return('success', [
            'form_fields' => method_exists($this, 'form_fields') ? $this->form_fields() : [],
            'el_table_column' => $this->el_table_column(),
            'search_fields' => $this->_searchField()
        ]);
    }

    // 保存数据
    public function save_data($args): array
    {
        $id = intval($args['id'] ?? 0);
        if ($id) {
            $data = $this->model->data_get(['id' => $id]);
            if (!$data) {
                return $this->error_return('数据不存在');
            }
        }
        if ($id) {
            $this->model->data_update(['id' => $id], $args);
        } else {
            unset($args['id']);
            $this->model->data_add($args);
        }
        return $this->success_return('保存成功');
    }

    // 表格列
    public function el_table_column(): array
    {
        return [
            ['prop' => 'id', 'type' => 'text', 'label' => 'id', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'title', 'type' => 'text', 'label' => '标题', 'minWidth' => '120', 'align' => 'center',],
            // ['prop' => 'content', 'type' => 'text', 'label' => '内容', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'created_at', 'type' => 'text', 'label' => '添加时间', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'updated_at', 'type' => 'text', 'label' => '最后修改', 'minWidth' => '120', 'align' => 'center',],
        ];
    }

    //列表搜索字段
    private function _searchField(): array
    {
        return [
            ['name' => 'title', 'label' => '标题', 'type' => 'input', 'placeholder' => '标题',],
            ['name' => 'created_at', 'label' => '添加时间', 'type' => 'daterange', 'placeholder' => '添加时间',],
        ];
    }

    // 获取公共条件限制
    private function _where($args = []): array
    {
        $where = [];
        if (!empty($args['title'])) {
            $where[] = ['title', 'like', '%' . $args['title'] . '%'];
        }
        if (!empty($args['created_at'])) {
            $where[] = ['created_at', 'between', [$args['created_at'][0] . ' 00:00:00', $args['created_at'][1] . ' 23:59:59']];
        }
        return $where;
    }

    public function list($args = []): array
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $sort_by = trim($args['sort_by'] ?? 'id desc');
        $where = $this->_where($args);
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', $sort_by, $page, $pageSize);
        if ($data) {
            foreach ($data as &$v) {
                $v['content'] = $v['content'] ? htmlspecialchars_decode(htmlspecialchars_decode($v['content'])) : "";
            }
            unset($v);
        }
        return $this->success_return('success', [
            'total' => $total,
            'list' => $data,
        ]);
    }


    //快速修改状态
    public function set_status($args): array
    {
        $id = intval($args['id'] ?? 0);
        if (!$id) {
            return $this->error_return('参数错误');
        }
        $data = $this->model->data_get(["id" => $id], 'status');
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        $res = (new NoteBooks())->data_update(["id" => $id], ['status' => $data['status'] == 1 ? -1 : 1]);
        if (!$res) {
            return $this->error_return('修改状态失败');
        }
        admin_log('修改状态', $args);
        return $this->success_return('保存成功');
    }

    // 删除数据
    public function del_data($args): array
    {
        $ids = $args['ids'] ?? [];
        if (!$ids) {
            $ids = !empty($args['id']) ? [$args['id']] : [];
        }
        if (!$ids) {
            return $this->error_return('请选择数据');
        }
        $data = $this->model->data_list_nopage([["id", 'in', $ids]], '*');
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        //判断是否存在del_time
        if (isset($data[0]['del_time'])) {
            $res = $this->model->data_update([["id", 'in', $ids]], ['del_time' => time()]);
        } else {
            $res = $this->model->data_del([["id", 'in', $ids]]);
        }
        if (!$res) {
            return $this->error_return('删除失败');
        }
        admin_log('删除笔记数据', $ids);
        return $this->success_return('删除成功');
    }


}
